package com.simtop.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.simtop.common.ServerResponse;
import com.simtop.pojo.User;
import com.simtop.pojo.experiment.ExperimentVo;
import com.simtop.pojo.experiment.ExperimentVoThree;
import com.simtop.pojo.experiment.ExperimentVoTwo;
import com.simtop.service.ExperimentService;
import com.simtop.util.ExcelUtils;
import com.simtop.util.JwtUtil;
import com.simtop.util.convert.Convert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.server.PathParam;
import java.util.List;

/**
 * @author lvyongwei
 */
@RestController
@CrossOrigin
@RequestMapping("/experiment")
public class ExperimentController {

    @Autowired
    private ExperimentService experimentService;

    @Autowired
    private ExcelUtils excelUtils;

    /**
     * 上传实验报告
     * @param request
     * @param experimentVoTwo
     * @return
     */
    @PostMapping("/add")
    public ServerResponse addExperiment(@RequestBody ExperimentVoTwo experimentVoTwo,HttpServletRequest request){
        String token = request.getHeader("Authorization");
        if(token == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        try{
            System.err.println(experimentVoTwo);
            ExperimentVoThree object = new ExperimentVoThree().getObject(experimentVoTwo);
            object.setUserId(u.getId());
            experimentService.insert(object);
            return ServerResponse.createBySuccessMsg("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return ServerResponse.createByErrorMsg("添加失败");
        }
    }

    /**
     * 查询列表
     * @return
     */
    @PostMapping("/getList")
    public ServerResponse getList(@RequestBody ExperimentVoThree experimentVoThree,HttpServletRequest request){
        String token = request.getHeader("Authorization");
        if(token == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        try{
            experimentVoThree.setRoleId(u.getRoleId());
            experimentVoThree.setUserId(u.getId());
            PageHelper.startPage(experimentVoThree.getPageNum(),experimentVoThree.getPageSize());
            List<ExperimentVo> all = experimentService.getAll(experimentVoThree);
            PageInfo<ExperimentVo> pageInfo = new PageInfo<ExperimentVo>(all);
            return ServerResponse.createBySuccess(pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            return ServerResponse.createByErrorMsg("查询失败");
        }
    }

    /**
     * 查询列表
     * @return
     */
    @PostMapping("/getExperiment")
    public ServerResponse getExperiment(@RequestBody String data,HttpServletRequest request){
        String token = request.getHeader("Authorization");
        if(token == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        try{
            JSONObject jsonObject = JSONObject.parseObject(data);
            Long experimentId = jsonObject.getLong("experimentId");
            ExperimentVoThree content = experimentService.getContent(experimentId);
            return ServerResponse.createBySuccess(content);
        }catch (Exception e){
            e.printStackTrace();
            return ServerResponse.createByErrorMsg("查询失败");
        }
    }


    /**
     * 删除实验报告
     * @return
     */
    @PostMapping("/delete")
    public ServerResponse delete(@RequestBody String data,HttpServletRequest request){
        String token = request.getHeader("Authorization");
        if(token == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        try{
            JSONObject jsonObject = JSONObject.parseObject(data);
            String experimentIds = jsonObject.getString("experimentId");
            Long[] longs = Convert.toLongArray(experimentIds);
            System.err.println(experimentIds);
            experimentService.delete(longs);
            return ServerResponse.createBySuccessMsg("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return ServerResponse.createByErrorMsg("删除失败");
        }
    }

    @GetMapping("/export")
    public void export(HttpServletRequest request, HttpServletResponse response, @PathParam("token")String token){
        User u = JwtUtil.unsign(token,User.class);
        ExperimentVoThree experimentVoThree = new ExperimentVoThree();
        experimentVoThree.setRoleId(u.getRoleId());
        experimentVoThree.setUserId(u.getId());
        List<ExperimentVo> all = experimentService.getAll(experimentVoThree);
        excelUtils.cooperationData(request,response,all,"实验报告",ExperimentVo.class);
    }
}
